/**
 * 文件上传控制器
 */
const formidable = require("formidable");

exports.uploadFile = (req, res) => {
  let form = new formidable.IncomingForm();
  form.encoding = "utf-8";
  form.uploadDir = __dirname + "/../uploads";
  form.keepExtensions = true;
  /**
   * err:错误信息对象
   * fields:普通键值对
   */
  form.parse(req, (err, fields, files) => {
    if (err) {
      res.json({
        code: 400,
        msg: "文件上传失败"
      });
    } else {
      // console.log(fields);
      // console.log(files);
      let imgname = path.basename(files.img.path);
      res.json({
        code: 200,
        msg: "文件上传成功",
        img: imgname
      });
    }
  });
};
